package com.dotstone.chipism.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import com.dotstone.chipism.R;
import com.dotstone.chipism.bean.Device;
import com.dotstone.chipism.bean.Key;
import com.dotstone.chipism.bean.KeyCmd;
import com.dotstone.chipism.bean.MySocket;
import com.dotstone.chipism.bean.MySocket.RecDataCallBackListener_TempHumity;
import com.dotstone.chipism.bean.Status;
import com.dotstone.chipism.util.ConvertUtil;
import com.dotstone.chipism.util.DeviceManager;
import com.dotstone.chipism.util.KeyManager;
import com.dotstone.chipism.util.MyDBHelper;
import com.dotstone.chipism.util.SocketManager;
import com.dotstone.chipism.util.ToastShow;
import com.dotstone.chipism.util.Util;
import com.dotstone.chipism.view.slidingfragmenu.BarUtils;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class RemoteAirActivity2 extends BaseActivity {
	private LinearLayout mBackL;
	private TextView mStatusBar;
	private Button mPowerB;
	private boolean on = false;
	private TextView mTitleTv;
	private Button mVoiceB;
	private Button mModeB;
	private Button mWindB;
	private Button mDerictionB;
	private Button mDerictionB1;
	private Button mWarmB;
	private Button mColdB;
	private Button mTimeB;
	private Button mMoreB;
	private RelativeLayout mF;
	private RelativeLayout mS;
	private Vibrator vibrator;
	private ImageView mShiImg;
	private ImageView mGeImg;
	private ImageView mModeImg;
	private MySocket socket;
	private int inner;
	private Dialog dialog;
	private GridView mKeysG;
	private KeyAdapter mAdapter;
	private static int dpi;
	private int o_x = 300;
	private int o_y = 302;
	private float current_degree;
	private int tempX1;
	private int tempY1;
	private int temp;
	private int temp1;
	private List<Button> buttons = new ArrayList<Button>();
	private List<Key> moreKeys = new ArrayList<Key>();
	private List<Key> keys = new ArrayList<Key>();
	private HashMap<Integer, String> cmds = new HashMap<Integer, String>();
	private Key modeKey = new Key();
	private Key windKey = new Key();
	private Key tempKey = new Key();
	private int tempPointer = 26;
	private int modelPointer = 1;
	private int windPointer = 1;
	private String tempcmd;
	private TextView wendu_tv;
	private TextView shidu_tv;
	private String wendu;
	private String shidu;
	private Device device;
	private MyDBHelper myDBHelper;
	private Status status;
	private boolean hasMode = false;
	private boolean hasWind = false;
	// private int mode;
	// private int wind;
	private double currentAngle = 355.5;

	@Override
	public void widgetClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back_layout:
			finish();
			break;
		case R.id.power_b:
			if (!on) {
				sendCmd(800);
				// resetButtons(true);
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				for (int i = 0; i < keys.size(); i++) {
					setBg(keys.get(i).getKeyNum(), !on);
				}
			} else {
				sendCmd(4000);
				Log.i("wmy", "reset Buttons 126");
				resetButtons(false);
			}
			on = !on;
			if (on) {
				myDBHelper.updateStatus(device.getDeviceID(), 1, tempPointer, windPointer, modelPointer);
			} else {
				myDBHelper.updateStatus(device.getDeviceID(), 0, tempPointer, windPointer, modelPointer);
			}
			break;
		case R.id.voice_btn:
			ToastShow.Show(getApplicationContext(), "尚未实现");
			break;
		case R.id.mode_btn:
			// 832
			Log.i("wmy", "141点击了模式 modeKey.getCmds().size()" + modeKey.getCmds().size());
			int size = modeKey.getCmds().size();
			if (modelPointer == 5) {
				modelPointer = 1;
			} else {
				modelPointer++;
			}
			for (int i = 0; i < size; i++) {
				Log.i("wmy", "modeKey.getCmds().get(i).getNum() = " + modeKey.getCmds().get(i).getNum());
				if (modelPointer == modeKey.getCmds().get(i).getNum()) {

					String testModelCmd = ConvertUtil.getInstance()
							.convertInfraredCmd(modeKey.getCmds().get(i).getCmd());
					socket.sendMqttData(testModelCmd);
					setModelBg(modelPointer);
					vibrator.vibrate(50);
					break;
				}
			}
			myDBHelper.updateStatus(device.getDeviceID(), 1, tempPointer, windPointer, modelPointer);
			break;
		case R.id.wind_speed_btn:
			// 833
			if (on) {
				int size1 = windKey.getCmds().size();
				if (windPointer == 5) {
					windPointer = 1;
				} else {
					windPointer++;
				}
				for (int i = 0; i < size1; i++) {
					if (windPointer == windKey.getCmds().get(i).getNum()) {
						String testModelCmd = ConvertUtil.getInstance()
								.convertInfraredCmd(windKey.getCmds().get(i).getCmd());
						setWindBg(windPointer);
						vibrator.vibrate(50);
						socket.sendMqttData(testModelCmd);
						break;
					}
				}
				myDBHelper.updateStatus(device.getDeviceID(), 1, tempPointer, windPointer, modelPointer);
			}
			break;
		case R.id.wind_deriction_btn:
			// 834 左右扫风
			sendCmd(834);
			break;
		case R.id.wind_deriction_btn1:
			// 835 上下扫风
			sendCmd(835);
			break;
		case R.id.warn_btn:
			// 883
			sendCmd(883);
			break;
		case R.id.cold_btn:
			// 882
			sendCmd(882);
			break;
		case R.id.time_btn:
			// 876
			sendCmd(876);
			break;
		case R.id.more_btn:
			initDialog();
			break;

		default:
			break;
		}
	}

	private void sendCmd(int id) {
		Log.i("wmy", "cmds.get(id) id = " + id + " " + cmds.get(id));
		if (cmds.get(id) != null) {
			socket.sendMqttData(cmds.get(id));
			vibrator.vibrate(50);
			if (id == 883) {
				mModeImg.setImageResource(R.drawable.hot);
			} else if (id == 882) {
				mModeImg.setImageResource(R.drawable.cold);
			}
		}
	}

	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				int[] temps = new int[2];
				temps = Util.getTempRes(msg.arg1);
				mShiImg.setImageResource(temps[0]);
				mGeImg.setImageResource(temps[1]);
				if (msg.arg2 == 0) {
					mModeImg.setImageResource(R.drawable.auto);
				} else if (msg.arg2 == 1) {
					mModeImg.setImageResource(R.drawable.hot);
				} else if (msg.arg2 == 2) {
					mModeImg.setImageResource(R.drawable.cold);
				} else if (msg.arg2 == 3) {
					mModeImg.setImageResource(R.drawable.dry);
				}
				break;
			case 1:
				wendu = socket.getTemp() + "";
				shidu = socket.getHumity() + "";
				wendu_tv.setText("当前温度：\n" + wendu + "℃");
				shidu_tv.setText("当前湿度：\n" + shidu);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void initParms(Bundle parms) {
		// TODO Auto-generated method stub

	}

	@Override
	public View bindView() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int bindLayout() {
		// TODO Auto-generated method stub
		return R.layout.activity_air;
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		dpi = DeviceManager.getInstance().getDEVICE_DENSITY_DPI();
		o_x = 309 * dpi / 320;
		o_y = 309 * dpi / 320;
		mBackL = $(R.id.back_layout);
		mStatusBar = $(R.id.tv_statusBar_mian);
		mTitleTv = $(R.id.title_tv);
		mPowerB = $(R.id.power_b);
		mVoiceB = $(R.id.voice_btn);
		mModeB = $(R.id.mode_btn);
		mWindB = $(R.id.wind_speed_btn);
		mDerictionB = $(R.id.wind_deriction_btn);
		mDerictionB1 = $(R.id.wind_deriction_btn1);
		mWarmB = $(R.id.warn_btn);
		mColdB = $(R.id.cold_btn);
		mTimeB = $(R.id.time_btn);
		mMoreB = $(R.id.more_btn);
		mShiImg = $(R.id.shi_img);
		mGeImg = $(R.id.ge_img);
		mModeImg = $(R.id.mode_img);
		wendu_tv = $(R.id.wendu);
		shidu_tv = $(R.id.shidu);
		mF = (RelativeLayout) findViewById(R.id.rl_father);
		mS = (RelativeLayout) findViewById(R.id.rl_mezi);
		vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
		int position = getIntent().getIntExtra("position", -1);
		if (position != -1) {
			device = DeviceManager.getInstance().getRemotes().get(position);
		} else {
			String id = getIntent().getStringExtra("id");
			device = DeviceManager.getInstance().getDeviceById(id);
		}
		myDBHelper = new MyDBHelper(getApplicationContext());
		status = myDBHelper.getStatusById(device.getDeviceID());
		keys = device.getKeys();
		// Log.i("wmy", "keys.size() = " + keys.size());
		mTitleTv.setText(device.getDeviceName());
		DeviceManager.getInstance().setmWlanId(device.getMainDeviceId());
		socket = SocketManager.getInstance().sMap.get(DeviceManager.getInstance().getmWlanId());
		initButtons();
		if (socket == null) {
			ToastShow.Show(getApplicationContext(), "请重新选择网关再添加！");
			finish();
		}
		initKey();
		Log.i("wmy", "resetButtons 320");
		resetButtons(false);
		initMoreKey();
		if (status != null) {
			initStatus();
		}
		boolVersion();
		checkCurrentState();
		setModelBg(modelPointer);
		mBackL.setOnClickListener(this);
		mPowerB.setOnClickListener(this);
		mVoiceB.setOnClickListener(this);
		mModeB.setOnClickListener(this);
		mWindB.setOnClickListener(this);
		mDerictionB.setOnClickListener(this);
		mDerictionB1.setOnClickListener(this);
		mWarmB.setOnClickListener(this);
		mColdB.setOnClickListener(this);
		mTimeB.setOnClickListener(this);
		mMoreB.setOnClickListener(this);
		socket = SocketManager.getInstance().sMap.get(DeviceManager.getInstance().getmWlanId());
		for (int i = 0; i < keys.size(); i++) {
			Log.i("wmy", "获取 " + keys.get(i).getName() + " " + keys.get(i).getKeyNum());
			setBg(keys.get(i).getKeyNum(), on);
		}
		if (socket != null) {
			setRecall();
		}
		mF.setOnTouchListener(new OnTouchListener() {
			// 根据角度 判断区域属于哪一个温度
			// 如果属于同一个温度 则不做处理
			// 如果属于不同温度 则赋值当前温度
			// 震动
			@Override
			public boolean onTouch(View v, MotionEvent e) {
				// TODO Auto-generated method stub
				int action = e.getAction();
				switch (action) {
				case MotionEvent.ACTION_DOWN:

					break;
				case MotionEvent.ACTION_MOVE:
					if (on && (modelPointer == 2 || modelPointer == 3)) {
						if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
							current_degree = detaDegree(o_x, o_y, e.getX(), e.getY());
							// 根据象限求坐标
							if (current_degree < 50 || current_degree > 130) {

								getButtonXY(current_degree);
								mS.setX(tempX1 - 24 * dpi / 320);
								mS.setY(tempY1 - 24 * dpi / 320);
								mS.postInvalidate();
							}
						} else {
							return false;
						}
					}
					break;
				case MotionEvent.ACTION_UP:
					if (on && (modelPointer == 2 || modelPointer == 3)) {
						if (adjustArea(o_x, o_y, e.getX(), e.getY())) {
							current_degree = detaDegree(o_x, o_y, e.getX(), e.getY());
							// 根据象限求坐标
							if (current_degree < 50 || current_degree > 130) {
								Log.i("wmy", "当前上提角度" + current_degree);
								adjustTemp(current_degree);
								getButtonXY(current_degree);
								mS.setX(tempX1 - 20 * dpi / 320);
								mS.setY(tempY1 - 20 * dpi / 320);
								mS.postInvalidate();
							}
						} else {
							return false;
						}
					}
					break;

				default:
					break;
				}
				return true;
			}
		});
	}

	private void initStatus() {
		if (status.isPower()) {
			on = true;
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			if (moreKeys.size() != 0) {
				mMoreB.setBackgroundResource(R.drawable.selector_more1);
			}
		} else {
			on = false;
			mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
		}
		switch (status.getTemp()) {
		case 16:
			mShiImg.setImageResource(R.drawable.ac_1);
			mGeImg.setImageResource(R.drawable.ac_6);
			temp = 16;
			currentAngle = 148;
			break;
		case 17:
			mShiImg.setImageResource(R.drawable.ac_1);
			mGeImg.setImageResource(R.drawable.ac_7);
			temp = 17;
			currentAngle = 168.5;
			break;
		case 18:
			mShiImg.setImageResource(R.drawable.ac_1);
			mGeImg.setImageResource(R.drawable.ac_8);
			temp = 18;
			currentAngle = 185.5;
			break;
		case 19:
			mShiImg.setImageResource(R.drawable.ac_1);
			mGeImg.setImageResource(R.drawable.ac_9);
			temp = 19;
			currentAngle = 202.5;
			break;
		case 20:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_0);
			temp = 20;
			currentAngle = 219.5;
			break;
		case 21:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_1);
			temp = 21;
			currentAngle = 236.5;
			break;
		case 22:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_2);
			temp = 22;
			currentAngle = 253.5;
			break;
		case 23:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_3);
			temp = 23;
			currentAngle = 270.5;
			break;
		case 24:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_4);
			temp = 24;
			currentAngle = 287.5;
			break;
		case 25:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_5);
			temp = 25;
			currentAngle = 304.5;
			break;
		case 26:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_6);
			temp = 26;
			currentAngle = 321.5;
			break;
		case 27:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_7);
			temp = 27;
			currentAngle = 338.5;
			break;
		case 28:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_8);
			temp = 28;
			currentAngle = 355.5;
			break;
		case 29:
			mShiImg.setImageResource(R.drawable.ac_2);
			mGeImg.setImageResource(R.drawable.ac_9);
			temp = 29;
			currentAngle = 11.5;
			break;
		case 30:
			mShiImg.setImageResource(R.drawable.ac_3);
			mGeImg.setImageResource(R.drawable.ac_0);
			temp = 30;
			currentAngle = 28.5;
			break;
		case 31:
			mShiImg.setImageResource(R.drawable.ac_3);
			mGeImg.setImageResource(R.drawable.ac_1);
			temp = 31;
			break;
		case 32:
			mShiImg.setImageResource(R.drawable.ac_3);
			mGeImg.setImageResource(R.drawable.ac_2);
			temp = 32;
			break;

		default:
			break;
		}
		switch (status.getWind()) {
		case 0:// LEVEL_1:
			mWindB.setText("一档");
			windPointer = 0;
			break;
		case 1:// LEVEL_2:
			mWindB.setText("二档");
			windPointer = 1;
			break;
		case 2:// LEVEL_3:
			mWindB.setText("三档");
			windPointer = 2;
			break;
		case 3:// LEVEL_4:
			mWindB.setText("四档");
			windPointer = 3;
			break;
		case 4:
			mWindB.setText("自动");
			windPointer = 4;
			break;
		}
		switch (status.getMode()) {
		case 1:// WIND
			mModeImg.setImageResource(R.drawable.quick_songfeng);
			modelPointer = 1;
			break;
		case 4:// COOL
			mModeImg.setImageResource(R.drawable.cold);
			modelPointer = 4;
			break;
		case 3:// HOT
			mModeImg.setImageResource(R.drawable.hot);
			modelPointer = 3;
			break;
		case 2:// DRY
			mModeImg.setImageResource(R.drawable.dry);
			modelPointer = 2;
			break;
		case 0:
			mModeImg.setImageResource(R.drawable.auto);
			modelPointer = 0;
			break;
		}
		getButtonXY(Float.parseFloat("" + currentAngle));
		mS.setX(tempX1 - 20 * dpi / 320);
		mS.setY(tempY1 - 20 * dpi / 320);
	}

	private void checkCurrentState() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while (true) {
					try {
						Thread.sleep(2000);
						mHandler.sendEmptyMessage(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	private boolean adjustArea(float src_x, float src_y, float target_x, float target_y) {
		inner = (int) Math.sqrt(Math.pow(Math.abs(target_x - src_x), 2) + Math.pow(Math.abs(target_y - src_y), 2));
		if (inner < 300 * dpi / 320 && inner > 90 * dpi / 320) {
			return true;
		} else {
			return false;
		}
	}

	float detaDegree(float src_x, float src_y, float target_x, float target_y) {
		float detaX = target_x - src_x;
		float detaY = target_y - src_y;
		double d;
		if (detaX != 0) {
			float tan = Math.abs(detaY / detaX);

			if (detaX > 0) {

				if (detaY >= 0) {
					d = Math.atan(tan);

				} else {
					d = 2 * Math.PI - Math.atan(tan);
				}

			} else {
				if (detaY >= 0) {

					d = Math.PI - Math.atan(tan);
				} else {
					d = Math.PI + Math.atan(tan);
				}
			}

		} else {
			if (detaY > 0) {
				d = Math.PI / 2;
			} else {

				d = -Math.PI / 2;
			}
		}
		return (float) ((d * 180) / Math.PI);
	}

	private void getButtonXY(float degree) {
		tempX1 = (int) (o_x + (150 * dpi / 320 * Math.cos(Math.toRadians(degree))));
		tempY1 = (int) (o_y + (150 * dpi / 320 * Math.sin(Math.toRadians(degree))));
	}

	private void adjustTemp(float degree) {

		if (130 < degree && degree <= 160) {
			if (temp != 16) {
				temp = 16;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_1);
				mGeImg.setImageResource(R.drawable.ac_6);
				vibrator.vibrate(50);
			}
		} else if (160 < degree && degree <= 177) {
			if (temp != 17) {
				temp = 17;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_1);
				mGeImg.setImageResource(R.drawable.ac_7);
				vibrator.vibrate(50);
			}
		} else if (177 < degree && degree <= 194) {
			if (temp != 18) {
				temp = 18;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_1);
				mGeImg.setImageResource(R.drawable.ac_8);
				vibrator.vibrate(50);
			}
		} else if (194 < degree && degree <= 211) {
			if (temp != 19) {
				temp = 19;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_1);
				mGeImg.setImageResource(R.drawable.ac_9);
				vibrator.vibrate(50);
			}
		} else if (211 < degree && degree <= 228) {
			if (temp != 20) {
				temp = 20;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_0);
				vibrator.vibrate(50);
			}
		} else if (228 < degree && degree <= 245) {
			if (temp != 21) {
				temp = 21;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_1);
				vibrator.vibrate(50);
			}
		} else if (245 < degree && degree <= 262) {
			if (temp != 22) {
				temp = 22;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_2);
				vibrator.vibrate(50);
			}
		} else if (262 < degree && degree <= 279) {
			if (temp != 23) {
				temp = 23;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_3);
				vibrator.vibrate(50);
			}
		} else if (279 < degree && degree <= 296) {
			if (temp != 24) {
				temp = 24;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_4);
				vibrator.vibrate(50);
			}
		} else if (296 < degree && degree <= 313) {
			if (temp != 25) {
				temp = 25;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_5);
				vibrator.vibrate(50);
			}
		} else if (313 < degree && degree <= 330) {
			if (temp != 26) {
				temp = 26;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_6);
				vibrator.vibrate(50);
			}
		} else if (330 < degree && degree <= 347) {
			if (temp != 27) {
				temp = 27;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_7);
				vibrator.vibrate(50);
			}
		} else if (347 < degree || degree <= 3) {
			if (temp != 28) {
				temp = 28;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_8);
				vibrator.vibrate(50);
			}
		} else if (3 < degree && degree <= 20) {
			if (temp != 29) {
				temp = 29;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_2);
				mGeImg.setImageResource(R.drawable.ac_9);
				vibrator.vibrate(50);
			}
		} else if (20 < degree && degree <= 50) {
			if (temp != 30) {
				temp = 30;
				sendCmd();
				temp1 = temp;
				mShiImg.setImageResource(R.drawable.ac_3);
				mGeImg.setImageResource(R.drawable.ac_0);
				vibrator.vibrate(50);
			}
		}
	}

	@Override
	public void setListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void doBusiness(Context mContext) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		DeviceManager.getInstance().setNeedToCheckTemp(true);
	}

	private void initKey() {
		// 遍历所有按键 找出模式 温度+ 温度- 以及风量
		for (int i = 0; i < keys.size(); i++) {
			Log.i("wmy", i + " " + keys.get(i).getKeyNum());
			if (keys.get(i).getKeyNum() == 800 || keys.get(i).getKeyNum() == 834 || keys.get(i).getKeyNum() == 832
					|| keys.get(i).getKeyNum() == 833 || keys.get(i).getKeyNum() == 835
					|| keys.get(i).getKeyNum() == 883 || keys.get(i).getKeyNum() == 882
					|| keys.get(i).getKeyNum() == 876) {
				String cmd = ConvertUtil.getInstance().convertInfraredCmd(keys.get(i).getCodeValue());
				cmds.put(keys.get(i).getKeyNum(), cmd);
			}
		}
		// 遍历所有按键 如果按键的keyNum大于10000 则是次级信号
		// if (windKey.getKeyNum() != 0) {
		Log.i("wmy", "有windKey = " + windKey.getKeyNum());
		for (int i = 0; i < keys.size(); i++) {
			if (keys.get(i).getKeyNum() > 10000) {
				if (keys.get(i).getKeyNum() == 10101) {
					KeyCmd keyCmd = new KeyCmd(2, 1, keys.get(i).getCodeValue());
					windKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10102) {
					KeyCmd keyCmd = new KeyCmd(2, 2, keys.get(i).getCodeValue());
					windKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10103) {
					KeyCmd keyCmd = new KeyCmd(2, 3, keys.get(i).getCodeValue());
					windKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10104) {
					KeyCmd keyCmd = new KeyCmd(2, 4, keys.get(i).getCodeValue());
					windKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10105) {
					KeyCmd keyCmd = new KeyCmd(2, 5, keys.get(i).getCodeValue());
					windKey.getCmds().add(keyCmd);
				}
			}
		}
		hasWind = true;
		Log.i("wmy", "有modeKey = " + modeKey.getKeyNum());
		for (int i = 0; i < keys.size(); i++) {
			if (keys.get(i).getKeyNum() > 10000) {
				if (keys.get(i).getKeyNum() == 10201) {
					KeyCmd keyCmd = new KeyCmd(1, 1, keys.get(i).getCodeValue());
					modeKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10202) {
					KeyCmd keyCmd = new KeyCmd(1, 2, keys.get(i).getCodeValue());
					modeKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10203) {
					KeyCmd keyCmd = new KeyCmd(1, 3, keys.get(i).getCodeValue());
					modeKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10204) {
					KeyCmd keyCmd = new KeyCmd(1, 4, keys.get(i).getCodeValue());
					modeKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10205) {
					KeyCmd keyCmd = new KeyCmd(1, 5, keys.get(i).getCodeValue());
					modeKey.getCmds().add(keyCmd);
				}
			}
		}
		hasMode = true;
		Log.i("wmy", "有tempKey = " + tempKey.getKeyNum());
		for (int i = 0; i < keys.size(); i++) {
			if (keys.get(i).getKeyNum() > 10000) {
				if (keys.get(i).getKeyNum() == 10016) {
					KeyCmd keyCmd = new KeyCmd(4, 16, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10017) {
					KeyCmd keyCmd = new KeyCmd(4, 17, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10018) {
					KeyCmd keyCmd = new KeyCmd(4, 18, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10019) {
					KeyCmd keyCmd = new KeyCmd(4, 19, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10020) {
					KeyCmd keyCmd = new KeyCmd(4, 20, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10021) {
					KeyCmd keyCmd = new KeyCmd(4, 21, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10022) {
					KeyCmd keyCmd = new KeyCmd(4, 22, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10023) {
					KeyCmd keyCmd = new KeyCmd(4, 23, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10024) {
					KeyCmd keyCmd = new KeyCmd(4, 24, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10025) {
					KeyCmd keyCmd = new KeyCmd(4, 25, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10026) {
					KeyCmd keyCmd = new KeyCmd(4, 26, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10027) {
					KeyCmd keyCmd = new KeyCmd(4, 27, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10028) {
					KeyCmd keyCmd = new KeyCmd(4, 28, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10029) {
					KeyCmd keyCmd = new KeyCmd(4, 29, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10030) {
					KeyCmd keyCmd = new KeyCmd(4, 30, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				} else if (keys.get(i).getKeyNum() == 10031) {
					KeyCmd keyCmd = new KeyCmd(4, 31, keys.get(i).getCodeValue());
					tempKey.getCmds().add(keyCmd);
				}
			}
		}
		// }

	}

	private void setWindBg(int keyNum) {
		switch (keyNum) {
		case 1:
			mWindB.setText("一档");
			break;
		case 2:
			mWindB.setText("二档");
			break;
		case 3:
			mWindB.setText("三档");
			break;
		case 4:
			mWindB.setText("四档");
			break;
		case 5:
			mWindB.setText("自动");
			break;

		default:
			break;
		}
	}

	private void setModelBg(int keyNum) {
		switch (keyNum) { // 1送风 2制冷 3取暖 4抽湿 5自动
		case 1:
			mModeImg.setImageResource(R.drawable.quick_songfeng);
			break;
		case 2:
			mModeImg.setImageResource(R.drawable.cold);
			break;
		case 3:
			mModeImg.setImageResource(R.drawable.hot);
			break;
		case 4:
			mModeImg.setImageResource(R.drawable.dry);
			break;
		case 5:
			mModeImg.setImageResource(R.drawable.auto);
			break;

		default:
			break;
		}
	}

	private void initButtons() {
		buttons.add(mModeB);
		buttons.add(mWindB);
		buttons.add(mDerictionB);
		buttons.add(mDerictionB1);
		buttons.add(mWarmB);
		buttons.add(mColdB);
		buttons.add(mTimeB);
		buttons.add(mMoreB);
	}

	private void initMoreKey() {
		// 在遥控器主面板已有的按键 832 833 834 835 883 882 876 800
		// moreKeys.add(keys.get(i));
		if (tempKey.getCmds().size() != 0) {
			Key key = new Key("温度+", 1, 811, false);
			Key key2 = new Key("温度-", 1, 812, false);
			moreKeys.add(key);
			moreKeys.add(key2);
		}
		for (int i = 0; i < keys.size(); i++) {
			if (keys.get(i).getKeyNum() != 800 && keys.get(i).getKeyNum() != 834 && keys.get(i).getKeyNum() != 832
					&& keys.get(i).getKeyNum() != 833 && keys.get(i).getKeyNum() != 835
					&& keys.get(i).getKeyNum() != 883 && keys.get(i).getKeyNum() != 882
					&& keys.get(i).getKeyNum() != 876 && keys.get(i).getKeyNum() != 811
					&& keys.get(i).getKeyNum() != 812 && keys.get(i).getKeyNum() != 4000
					&& keys.get(i).getKeyNum() < 10000) {
				moreKeys.add(keys.get(i));
			}
		}
		if (on) {
			mMoreB.setBackgroundResource(R.drawable.selector_more1);
		}
		Log.i("wmy", 987 + " initMoreKeys = " + moreKeys.size() + " on = " + on);
	}

	private void sendCmd() {
		tempPointer = temp;
		for (int i = 0; i < tempKey.getCmds().size(); i++) {
			if (temp == tempKey.getCmds().get(i).getNum()) {
				tempcmd = ConvertUtil.getInstance().convertInfraredCmd(tempKey.getCmds().get(i).getCmd());
				socket.sendMqttData(tempcmd);
				break;
			}
		}
		// socket.sendMqttData(cmd);
	}

	private void setRecall() {
		socket.setRecDataCallBackListener_TempHumity(new RecDataCallBackListener_TempHumity() {

			@Override
			public void onReceiveData(String data) {
				// TODO Auto-generated method stub
				parseData(data);
			}
		});
	}

	private void parseData(String data) {
		try {
			JSONObject jo = new JSONObject(data);
			String g = jo.getString("g");
			JSONObject jo1 = new JSONObject(g);
			int temperature = (int) jo1.getDouble("temperature");
			Message msg = Message.obtain();
			msg.what = 0;
			msg.arg1 = temperature;
			mHandler.sendMessage(msg);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void initDialog() {
		// TODO Auto-generated method stub
		dialog = new Dialog(RemoteAirActivity2.this, R.style.MyDialogStyle);
		dialog.setContentView(R.layout.dialog_more_key);
		Window dialogWindow = dialog.getWindow();
		dialog.setCancelable(true);
		dialog.setCanceledOnTouchOutside(true);
		mKeysG = (GridView) dialog.findViewById(R.id.key_gv);
		LinearLayout layout = (LinearLayout) dialog.findViewById(R.id.main);
		layout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		mAdapter = new KeyAdapter();
		mKeysG.setAdapter(mAdapter);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = LayoutParams.FILL_PARENT;
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	public void boolVersion() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

			Window window = getWindow();
			window.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
					WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
			// 透明导航栏
			// getWindow().addFlags(
			// WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
			// 改变titlebar的高度
			int statusbarHeight = BarUtils.getStatusBarHeight(this);
			RelativeLayout.LayoutParams layoutParams1 = (RelativeLayout.LayoutParams) mStatusBar.getLayoutParams();
			layoutParams1.height = statusbarHeight;
			mStatusBar.setLayoutParams(layoutParams1);
			mStatusBar.setVisibility(View.VISIBLE);
		} else {
			mStatusBar.setVisibility(View.GONE);
		}
	}

	private void setBg(int id, boolean on) {
		Log.i("wmy", "setBg id = " + id);
		switch (id) {
		case 800:
			mPowerB.setEnabled(true);
			if (on) {
				mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
				if (moreKeys.size() != 0) {
					Log.i("wmy", "1078 more_btn set bg");
					mMoreB.setBackgroundResource(R.drawable.selector_more1);
					mMoreB.setEnabled(true);
				}
				if (hasWind) {
					mWindB.setEnabled(true);
					mWindB.setBackgroundResource(R.drawable.kuang2x);
					mWindB.setTextColor(Color.parseColor("#3fb6d5"));
					// mWindB.setBackgroundResource(R.drawable.selector_wind_speed);
					// mWindB.setText(text);
				}
				if (hasMode) {
					mModeB.setEnabled(true);
					mModeB.setBackgroundResource(R.drawable.selector_mode);
				}
			} else {
				mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
			}
			break;
		case 833:
			mWindB.setEnabled(true);
			mWindB.setBackgroundResource(R.drawable.kuang2x);
			if (on) {
				mWindB.setTextColor(Color.parseColor("#3fb6d5"));
			} else {
				mWindB.setTextColor(Color.parseColor("#b9b9b9"));
			}
			// mWindB.setBackgroundResource(R.drawable.selector_wind_speed);
			break;
		case 834:
			mDerictionB.setEnabled(true);
			if (on) {
				mDerictionB.setBackgroundResource(R.drawable.selector_deriction);
			} else {
				mDerictionB.setBackgroundResource(R.drawable.xiabai12x);
			}
			break;
		case 835:
			mDerictionB1.setEnabled(true);
			if (on) {
				mDerictionB1.setBackgroundResource(R.drawable.selector_deriction1);
			} else {
				mDerictionB1.setBackgroundResource(R.drawable.shangbai12x);
			}
			break;
		case 883:
			mWarmB.setEnabled(true);
			if (on) {
				mWarmB.setBackgroundResource(R.drawable.selector_warm);
			} else {
				mWarmB.setBackgroundResource(R.drawable.zhire12x);
			}
			break;
		case 882:
			mColdB.setEnabled(true);
			if (on) {
				mColdB.setBackgroundResource(R.drawable.selector_cold);
			} else {
				mColdB.setBackgroundResource(R.drawable.zhileng12x);
			}
			break;
		case 876:
			mTimeB.setEnabled(true);
			if (on) {
				mTimeB.setBackgroundResource(R.drawable.selector_time);
			} else {
				mTimeB.setBackgroundResource(R.drawable.naozhong12x);
			}
			break;
		default:
			break;
		}
	}

	private void resetButtons(boolean on) {
		if (on) {
			mPowerB.setBackgroundResource(R.drawable.kaiguan12x);
			mModeB.setEnabled(true);
			mModeB.setBackgroundResource(R.drawable.selector_mode);
			// mWindB.setEnabled(true);
			mWindB.setBackgroundResource(R.drawable.kuang2x);
			mWindB.setTextColor(Color.parseColor("#3fb6d5"));
			mDerictionB.setEnabled(true);
			mDerictionB.setBackgroundResource(R.drawable.selector_deriction);
			mDerictionB1.setEnabled(true);
			mDerictionB1.setBackgroundResource(R.drawable.selector_deriction1);
			mWarmB.setEnabled(true);
			mWarmB.setBackgroundResource(R.drawable.selector_warm);
			mColdB.setEnabled(true);
			mColdB.setBackgroundResource(R.drawable.selector_cold);
			mTimeB.setEnabled(true);
			mTimeB.setBackgroundResource(R.drawable.selector_time);
			Log.i("wmy", 1118 + " moreKeys.size() =  " + moreKeys.size());
			if (moreKeys.size() != 0) {
				mMoreB.setBackgroundResource(R.drawable.selector_more1);
				mMoreB.setEnabled(true);
			}
		} else {
			mPowerB.setBackgroundResource(R.drawable.kaiguan22x);
			mModeB.setBackgroundResource(R.drawable.moshi1x);
			mModeB.setEnabled(false);
			mWindB.setBackgroundResource(R.drawable.kuang2x);
			// mWindB.setEnabled(false);
			mWindB.setTextColor(Color.parseColor("#b9b9b9"));
			mDerictionB.setBackgroundResource(R.drawable.xiabai12x);
			mDerictionB.setEnabled(false);
			mDerictionB1.setBackgroundResource(R.drawable.shangbai12x);
			mDerictionB1.setEnabled(false);
			mWarmB.setBackgroundResource(R.drawable.zhire12x);
			mWarmB.setEnabled(false);
			mColdB.setBackgroundResource(R.drawable.zhileng12x);
			mColdB.setEnabled(false);
			mTimeB.setBackgroundResource(R.drawable.naozhong12x);
			mTimeB.setEnabled(false);
			Log.i("wmy", "set MoreBtn unclickable");
			mMoreB.setBackgroundResource(R.drawable.ddd12x);
			mMoreB.setEnabled(false);
		}
	}

	class KeyAdapter extends BaseAdapter {
		HoldView holdView;
		private int imageResId;
		private int stringResId;

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return moreKeys.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return moreKeys.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			// TODO Auto-generated method stub
			if (v == null) {
				v = LayoutInflater.from(RemoteAirActivity2.this).inflate(R.layout.item_more_key, null);
				holdView = new HoldView();
				holdView.btn = (Button) v.findViewById(R.id.key_btn);
				v.setTag(holdView);
			} else {
				holdView = (HoldView) v.getTag();
			}
			final Key k = moreKeys.get(position);
			holdView.btn.setTag(k.getKeyNum());
			int content = KeyManager.getInstance().getContentTypeByKeyNum(k.getKeyNum());
			if (content == 0) {
				imageResId = KeyManager.getInstance().getDrawableResId(k.getKeyNum());
				holdView.btn.setBackgroundResource(imageResId);
			} else {
				stringResId = KeyManager.getInstance().getStringResId(k.getKeyNum());
				holdView.btn.setText(stringResId);
			}
			holdView.btn.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Log.i("wmy", "点击了" + k.getKeyNum() + " " + k.getName() + " " + tempPointer);
					int tag = Integer.parseInt(v.getTag().toString());
					if (tag == 811) {

						if (tempPointer == 31) {
							String ir_data = tempKey.getCmds().get(tempKey.getCmds().size() - 1).getCmd();
							tempcmd = ConvertUtil.getInstance().convertInfraredCmd(ir_data);
							socket.sendMqttData(tempcmd);
							vibrator.vibrate(50);
						} else {
							tempPointer++;
							List<KeyCmd> cmds = tempKey.getCmds();
							boolean e = false;
							for (int i = 0; i < cmds.size(); i++) {
								if (tempPointer == cmds.get(i).getNum()) {
									tempcmd = ConvertUtil.getInstance().convertInfraredCmd(cmds.get(i).getCmd());
									e = true;
									break;
								}
							}
							if (e) {
								socket.sendMqttData(tempcmd);
								vibrator.vibrate(50);
							} else {
								if (tempPointer >= cmds.get(cmds.size() - 1).getNum()) {
									tempcmd = ConvertUtil.getInstance()
											.convertInfraredCmd(cmds.get(cmds.size() - 1).getCmd());
									socket.sendMqttData(tempcmd);
									vibrator.vibrate(50);
								}
							}
						}
					} else if (tag == 812) {
						if (tempPointer == 16) {
							String ir_data = tempKey.getCmds().get(0).getCmd();
							tempcmd = ConvertUtil.getInstance().convertInfraredCmd(ir_data);
							socket.sendMqttData(tempcmd);
							vibrator.vibrate(50);
						} else {
							tempPointer--;
							List<KeyCmd> cmds = tempKey.getCmds();
							boolean e = false;
							for (int i = 0; i < cmds.size(); i++) {
								if (tempPointer == cmds.get(i).getNum()) {
									tempcmd = ConvertUtil.getInstance().convertInfraredCmd(cmds.get(i).getCmd());
									e = true;
									break;
								}
							}
							if (e) {
								socket.sendMqttData(tempcmd);
								vibrator.vibrate(50);
							} else {
								if (tempPointer <= cmds.get(0).getNum()) {
									tempcmd = ConvertUtil.getInstance().convertInfraredCmd(cmds.get(0).getCmd());
									socket.sendMqttData(tempcmd);
									vibrator.vibrate(50);
								}
							}
						}
					} else {
						sendCmd(tag);
						vibrator.vibrate(50);
					}
				}
			});

			return v;
		}
	}

	class HoldView {
		private Button btn;
	}

}